# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import csv

# useful for handling different item types with a single interface
from itemadapter import ItemAdapter


class JdSpiderPipeline:
    def open_spider(self, spider):
        self.f = open("jd.csv", "a")
        self.c = open("pinglun.csv", "a")
        self.cs = csv.DictWriter(self.f, ["title", "price", "name", "evaluate"])
        self.csf = csv.DictWriter(self.c, ["nickname", "referenceName", "content", "time"])
        self.cs.writeheader()
        self.csf.writeheader()

    def close_spider(self, spider):
        self.f.close()
        self.c.close()

    def process_item(self, item, spider):
        if item["type"] == "shangping":
            item.pop("type")
            self.cs.writerow(item)
        else:
            item.pop("type")
            self.csf.writerow(item)
        return item
